package com.yeci.controller;

import com.github.pagehelper.PageInfo;
import com.yeci.common.controller.BaseController;
import com.yeci.common.page.TableDataInfo;
import com.yeci.entity.SysBlog;
import com.yeci.common.AjaxResult;
import com.yeci.mapper.SysBlogMapper;
import com.yeci.service.SysBlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yeci
 * @since 2021-08-12
 */
@RestController
@Slf4j
@RequestMapping("/sysBlog")
public class SysBlogController extends BaseController {

  @Autowired
  private SysBlogMapper blogMapper;
  @Autowired
  private SysBlogService blogService;

  /**
   * 查询博客列表
   * @param sysBlog
   * @return
   */
  @GetMapping("/list")
  public TableDataInfo getBlogList(SysBlog sysBlog) {
    List<SysBlog> blogList = blogService.selectSysBlogList(sysBlog);
    return getDataTable(blogList);
  }

  /**
   * 根据Id查询博客信息
   * @param id 博客id
   * @return
   */
  @GetMapping(value = "/selectFindById/{id}")
  public AjaxResult getFindById(@PathVariable("id") String id){
    return AjaxResult.success(blogMapper.selectById(id));
  }

  /**
   * 添加博客
   * @param sysBlog 博客信息
   */
  @RequestMapping(value = "/addBlog",method = RequestMethod.POST)
  public AjaxResult addBlog(@RequestBody SysBlog sysBlog){
    return AjaxResult.toAjax(blogMapper.insert(sysBlog));
  }

  /**
   * 修改博客
   * @param sysBlog 博客信息
   */
  @RequestMapping(value = "/updateBlog",method = RequestMethod.POST)
  public AjaxResult updateBlog(@RequestBody SysBlog sysBlog){
    return AjaxResult.toAjax(blogMapper.updateById(sysBlog));
  }

  /**
   * 根据Id删除博客信息
   * @param id 博客id
   * @return
   */
  @GetMapping(value = "/deleteBlog/{id}")
  public AjaxResult deleteBlog(@PathVariable("id") String id){
    return AjaxResult.toAjax(blogMapper.deleteById(id));
  }

  /**
   * 查询逻辑删除的全部blog
   * @return
   */
  @RequestMapping("/deleteBlogList")
  public AjaxResult deleteBlogList(){
    List<SysBlog> blogList = blogMapper.deleteBlogList();
    return AjaxResult.success("操作成功",blogList,new PageInfo(blogList).getTotal());
  }

  /**
   * 根据Id彻底删除博客信息
   * @param id 博客id
   * @return
   */
  @GetMapping(value = "/completelyDelete/{id}")
  public AjaxResult completelyDelete(@PathVariable("id") String id){
    return AjaxResult.toAjax(blogMapper.deleteFindById(id));
  }

  /**
   * 根据Id还原博客信息
   */
  @GetMapping(value = "/recovery/{id}")
  public AjaxResult recoveryById(@PathVariable("id") String id){
    return AjaxResult.toAjax(blogMapper.recoveryById(id));
  }
}

